#include "variableDefine.h"
#include "commandFun.h"
#include <WString.h>

/////////////////////////////////////////////////////////////////////////////////////////////
// 定义指令
static const char cmd_ssids[] = "SSIDS";
static const char cmd_ssid[] = "SSID";
static const char cmd_on[] = "ON";
static const char cmd_off[] = "OFF";
static const char cmd_state[] = "STATE";
static const char cmd_list[] = "LIST";
static const char cmd_restart[] = "RESTART";
/////////////////////////////////////////////////////////////////////////////////////////////
// 定义指令
int command_size = 0;
command commands[] = {
    {cmd_ssids, strlen(cmd_ssids), getssids},
    {cmd_ssid, strlen(cmd_ssid), getssid},
    {cmd_on, strlen(cmd_on), pinon},
    {cmd_off, strlen(cmd_off), pinoff},
    {cmd_state, strlen(cmd_state), pinstate},
    {cmd_list, strlen(cmd_list), pinslist},
    {cmd_restart, strlen(cmd_restart), restart},
};
/////////////////////////////////////////////////////////////////////////////////////////////
// 定义路由密码
static const char sspwd_comm[] = "1qazxsw2";
static const char sspwd_ly[] = "qwer1234";

static const char ssid_ly[] = "ly_wifi";
static const char ssid_mzt[] = "MERCURY_zt";
static const char ssid_nc[] = "Netcore";
static const char ssid_xm[] = "Xiaomi";
static const char ssid_xmp[] = "Xiaomi_plus";
static const char ssid_xmz[] = "Xiaomi_zt";

// 定义路由密码信息
int ssidinfo_size = 0;
ssidinfo ssidinfos[] = {
    {ssid_ly, sspwd_ly},
    {ssid_mzt, sspwd_comm},
    {ssid_nc, sspwd_comm},
    {ssid_xm, sspwd_comm},
    {ssid_xmp, sspwd_comm},
    {ssid_xmz, sspwd_comm},
};
const char* ssidnames[] = {ssid_ly, ssid_mzt, ssid_nc, ssid_xm, ssid_xmp, ssid_xmz};

////////////////////////////////////////////////////////////////////////////////////////////////////
// 端口映射定义
//D3 D4 D5 D6 D7 D8 D9 D10 D11 D12 A0 A1 A2 A3
// 1 2  3  4  5  6  7  8   9    10 11 12 13 14

int pin_size = 0;
pinState pinlist[] = {
    { D3, false }, { D4, false },
    { D5, false }, { D6, false },
    { D7, false }, { D8, false },
    { D9, false }, { D10, false },
    { D11, false }, { D12, false },
    { A0, false }, { A1, false },
    { A2, false }, { A3, false },
};

////////////////////////////////////////////////////////////////////////////////////////////////////
void variableInit() {
    command_size = sizeof(commands) / sizeof(command);
    ssidinfo_size = sizeof(ssidinfos) / sizeof(ssidinfo);
    pin_size = sizeof(pinlist) / sizeof(pinState);
}

/////////////////////////////////////////////////////////////////////////////////////////////
WifiSSID* wifi_ssids = nullptr;
int wifi_ssid_cache = 0;
int wifi_ssid_len = 0;
int wifi_choose = -1;
/////////////////////////////////////////////////////////////////////////////////////////////
const char* selectAP(SerialESP8266wifi& wifi) {
    wifi_choose = -1;
    wifi_ssid_len = 0;
    if (wifi_ssids != nullptr) {
        for (auto i = 0; i < wifi_ssid_cache; i++) {
            auto& tssid = wifi_ssids[i];
            tssid.ssid = nullptr;
            tssid.rssi = 0;
        }
    } else {
        wifi_ssid_cache = 5;
        wifi_ssids = new WifiSSID[wifi_ssid_cache];
        for (auto i = 0; i < wifi_ssid_cache; i++) {
            auto& tssid = wifi_ssids[i];
            tssid.ssid = nullptr;
            tssid.rssi = 0;
        }
    }
    wifi_ssid_len = wifi.getSSIDs(wifi_ssids, wifi_ssid_cache, ssidnames, ssidinfo_size);
    if (wifi_ssid_len > 0) {
        int rssimax = -500;
        for (auto i = 0; i < wifi_ssid_len; i++) {
            auto& tssid = wifi_ssids[i];
            if (tssid.rssi != 0 && tssid.rssi > rssimax) {
                rssimax = tssid.rssi;
                wifi_choose = i;
            }
        }
    }
    if (wifi_choose != -1) {
        return wifi_ssids[wifi_choose].ssid;
    }
    return nullptr;
}

const char* getAPPwd(SerialESP8266wifi& wifi, const char* ssid) {
    if (ssid == nullptr) {
        return nullptr;
    }
    for (auto i = 0; i < ssidinfo_size; i++) {
        auto& tssid = ssidinfos[i];
        if (wifi.checkBuffer(tssid.ssid, ssid) == true) {
            return tssid.pwd;
        }
    }
    return nullptr;
}

void getssids(SerialESP8266wifi& wifi, char chann, const char* msg) {
    String ssids;
    for (auto i = 0; i < wifi_ssid_len; i++) {
        auto& tssid = wifi_ssids[i];
        ssids = ssids + "{\"ssid\":\"" + tssid.ssid + "\", \"rssi\":\"" + tssid.rssi + "\"}";
        if (i != wifi_ssid_len - 1) {
            ssids += ",";
        }
    }
    wifi.send(chann, String("[") + ssids + "]");
}

void getssid(SerialESP8266wifi& wifi, char chann, const char* msg) {
    if (wifi_choose >= 0 && wifi_choose < wifi_ssid_len) {
        auto& tssid = wifi_ssids[wifi_choose];
        wifi.send(chann, String("{\"ssid\":\"") + tssid.ssid + "\", \"rssi\":\"" + tssid.rssi + "\"}");
    } else {
        wifi.send(chann, "not find");
    }
}